package com.itheima.health.mobile.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.mobile.utils.SMSUtils;
import com.itheima.health.mobile.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @autahor: csl
 * @date: 2019/7/23
 * @description: 验证码 控制器
 * @version:1.0
 */
@SuppressWarnings("Duplicates")
@RestController
@RequestMapping("/mobile/validateCode")
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
         try {
             String code = ValidateCodeUtils.generateValidateCode4String(4);
             System.out.println("======================================"+code);
             //SMSUtils.sendShortMessage(telephone,code);
             jedisPool.getResource().setex(telephone+ RedisConst.SENDTYPE_ORDER,5*60,code);
             return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

         }catch (Exception e){
                     e.printStackTrace();
             return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
                 }
    }
    @RequestMapping("/send4Login")
    public Result send4login(String telephone){
        try {
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            System.out.println("======================================"+code);
            //SMSUtils.sendShortMessage(telephone,code);
            jedisPool.getResource().setex(telephone+ RedisConst.SENDTYPE_LOGIN,5*60,code);
            return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }
}
